﻿using HuaweiCloud.Dotnet.Samples.Common;

namespace HuaweiCloud.Dotnet.Samples.ESports.Services
{
    /// <summary>
    /// 包含增删改查的基本服务抽象类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class CurdServiceBase<T> : ICurdService<T> where T : class, IAggregateRoot
    {
        private readonly IRepository<T> _repository;
        public CurdServiceBase(IRepository<T> repository)
        {
            _repository = repository;
        }

        public virtual async Task AddAsync(T t)
        {
            await _repository.AddAsync(t);
        }

        public virtual async Task DeleteAsync(int id)
        {
            var item = await _repository.GetByIdAsync(id);
            if(item != null)
                await _repository.DeleteAsync(item);
        }

        public virtual async Task<T> GetAsync(int id)
        {
            return await _repository.GetByIdAsync(id);
        }

        public virtual async Task<IList<T>> ListAsync()
        {
            return await _repository.ListAsync();
        }

        public virtual async Task UpdateAsync(T t)
        {
            await _repository.UpdateAsync(t);
        }
    }
}
